
/**
 * Copyright (c) 2012 Kyle Hughart
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Credit for algorithm goes to:
 *
 * Kocsis L. & Szepesvari C. (September 2006). Bandit based Monte-Carlo
 * Planning. Unpublished paper presented European Conference on Machine
 * Learning, Berlin, Germany.
 *
 * Chaslot, Guillaume et al. (October, 2008). Monte-Carlo Tree Search: A New
 * Framework for Game AI. Unpublished paper presented at the Fourth Artificial
 * Intelligence and Interactive Digital Entertainment Conference, Maastricht,
 * The Netherlands.
 */
/**
 * HumanPlayer is a very simple Player class that reads integers from standard
 * input as the index of the desired move, given the list of possible moves
 * generated by the instance of Game being played.
 *
 * @author Kyle
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HumanPlayer implements Player
{

   private Game g;
   private boolean player1;
   GameState curState;

   public HumanPlayer(Game g, boolean player1)
   {
      this.g = g;
      this.player1 = player1;
      curState = g.getStartingState();

   }

   @Override
   public GameState getCurState()
   {
      return curState;
   }

   @Override
   public void updateGameState(GameState s)
   {
      curState = s;
   }

   /**
    * Makes a move by prompting the player for a move number, and using that
    * number for an index when selecting from the list of available legal moves.
    */
   @Override
   public void MakeMove()
   {
      if (g.gameStatus(curState) == Game.status.ONGOING) {
         try {
            ArrayList<? extends GameState> moves = g.getPossibleMoves(curState);
            System.out.print("Enter Move #: ");
            BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));
            String moveStr = inputStream.readLine();
            int move = Integer.parseInt(moveStr) % moves.size();
            curState = moves.get(move);
            System.in.read();
         } catch (IOException ex) {
            Logger.getLogger(HumanPlayer.class.getName()).log(Level.SEVERE, null, ex);
         }
      }
//      if (player1) {
//         System.out.print("player1 : ");
//      } else {
//         System.out.print("player2 : ");
//      }
//      System.out.println(g.gameStatus(curState));
//      g.printState(curState);
   }
}
